package com.huige.controller;

import com.huige.pojo.Schedule;
import com.huige.service.ScheduleService;
import com.huige.utils.PageBean;
import com.huige.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 朱志辉
 * @Create 2024/7/21 下午2:08
 * @Version 1.0
 * Description:
 */
@CrossOrigin//允许其他源访问我们的controller 浏览器不拦截
@RestController
@RequestMapping("schedule")
@Slf4j
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;


    //接受参数
    //响应结果 springmvc

    /**
     * 查询数据
     * @param pageSize
     * @param currentPage
     * @return
     */
    @GetMapping("/{pageSize}/{currentPage}")
    public R showSchedule(@PathVariable(value = "pageSize") int pageSize,
                          @PathVariable(value = "currentPage") int currentPage) {

        R r = scheduleService.page(pageSize, currentPage);
        log.info("查询结果：{}", r);
        return r;
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable(value = "id") Integer id) {
        //调用业务成方法
       R r  = scheduleService.deleteById(id);
        return r;
    }

    /**
     * 添加数据
     * @param schedule
     * @return
     */
    @PostMapping
    public R addSchedule(@Validated  @RequestBody Schedule schedule, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return R.fail("参数为null，不能保存");
        }
        R r = scheduleService.addSchedule(schedule);
        return r;
    }

    /**
     * 根据id修改数据
     * @param schedule
     * @return
     */
    @PutMapping
    public R update(@RequestBody Schedule schedule) {
        R r =scheduleService.update(schedule);
        return r;
    }

}
